<template>
  <div>
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body @close="cancel">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商品编码" prop="goodsId">
          <el-input v-model="form.goodsId" placeholder="请输入商品编码" @blur="goodsIdBlur" :disabled="detailId != null || goodsInfo != null" />
        </el-form-item>
        <el-form-item label="商品名称" prop="goodsName">
          <el-input v-model="form.goodsName" placeholder="请输入商品名称" disabled />
        </el-form-item>
        <el-form-item label="判责金额" prop="price">
          <el-input type="number" v-model="form.price" placeholder="请输入判责金额" :disabled="form.hasState === 1" />
        </el-form-item>
        <el-form-item label="被判责人" prop="judgeUserId">
          <el-select v-model="form.judgeUserId" placeholder="请选择被判责人" filterable @change="judgeUserChange" :disabled="detailId != null">
            <el-option v-for="item in userList" :label="item.nickName" :value="item.userId" />
          </el-select>
        </el-form-item>
        <el-form-item label="判责原因" prop="remark">
          <el-input type="textarea" v-model="form.remark" placeholder="请输入判责原因" :disabled="form.hasState === 1" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {getObligation, addObligation, updateObligation } from "@/api/other/obligation";
import {getGoods} from "@/api/recovery/goods";
import {listUser} from "@/api/system/user";

export default {
  name: "Obligation-form",
  props: {
    open: {type: Boolean},
    title: {type:String},
    detailId: {type: Number},
    goodsInfo: {type: Object}
  },
  watch: {
    open(val) {
      if(val)
        this.init();
    }
  },
  data() {
    return {
      userList: [],
      //0=判责中，1=已判责，2=作废
      statusOptions: [
        {v: 0, l : '判责中', t: 'warning'},
        {v: 1, l : '已判责', t: 'danger'},
        {v: 2, l : '作废', t: 'info'},
      ],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        status: [
          { required: true, message: "状态 0=判责中，1=已判责，2=作废不能为空", trigger: "blur" }
        ],
        hasState: [
          { required: true, message: "是否申述不能为空", trigger: "blur" }
        ],
      }
    }
  },
  created() {
    listUser({}).then(res => {
      this.userList = res.rows
    })
  },
  methods: {
    judgeUserChange() {
      this.userList.forEach(it => {
        if(it.userId === this.form.judgeUserId) {
          this.form.judgeUserName = it.nickName;
        }
      })
    },
    goodsIdBlur() {
      if(this.form.goodsId) {
        getGoods(this.form.goodsId).then(res => {
          this.form.goodsName = res.data.goodsName;
        })
      }

    },
    init() {
      this.reset();
      //获取详情
      if(this.detailId) {
        getObligation(this.detailId).then(response => {
          this.form = response.data;
        });
      }

      if(this.goodsInfo) {
        this.form.goodsId = this.goodsInfo.goodsId;
        this.form.goodsName = this.goodsInfo.goodsName;
      }
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        goodsId: null,
        goodsName: null,
        price: null,
        judgeUserId: null,
        judgeUserName: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateObligation(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addObligation(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    // 取消按钮
    cancel() {
      this.$emit("close");
      this.reset();
    },
  }
}
</script>
<style scoped lang="scss">

</style>
